package ryarithsGame;

import jmetest.renderer.TestEnvMap;

import com.jme.image.Texture;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Torus;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

public class Goal extends Node
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Vector3f goalLoc;
	Node goal;
	boolean showSwitch, isSwitched;
	Vector2f switchPos;
	
	public Goal(String name, Vector3f loc)
	{
		super(name);
		goalLoc = new Vector3f(loc);
		goalLoader();
		showSwitch = true;
		isSwitched = false;
		switchPos = new Vector2f(loc.x, loc.z);
	}
	
	public void goalLoader()
	{
		Torus torus = new Torus("Torus", 10, 10, 10, 20);
		
		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        // Base texture, not environmental map.
        Texture t0 = TextureManager.loadTexture(
                TestEnvMap.class.getClassLoader().getResource(
                        "data/images/DragonGames.png"),
                Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear);
        // Environmental Map (reflection of clouds)
        Texture t = TextureManager.loadTexture(TestEnvMap.class
                .getClassLoader()
                .getResource("data/textures/clouds.png"),
                Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear);
        t.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.SphereMap);
        ts.setTexture(t0, 0);
        ts.setTexture(t, 1);
        ts.setEnabled(true);
        
        torus.setLocalScale(.25f);
        
        goal = new Node("tower");
        goal.attachChild(torus);
        goal.setRenderState(ts);
        
        goal.setLocalTranslation(goalLoc);
        
        this.attachChild(goal);

	}
	public void setShowSwitch(boolean set)
	{
		showSwitch = set;
	}
	
	public boolean getIsSwitched()
	{
		return isSwitched;
	}
	
	public void checkSwitchCollisions(Vector2f checkVec)
	{
		//check switch
		if(switchPos.distance(checkVec) <= 5 && isSwitched == false)
		{
			if(showSwitch)
			{
				isSwitched = true;
			}
		}
	}
	public void updateGoalNode()
	{
		if(isSwitched)
		{
			showSwitch = false;
		}
		
		if(!showSwitch)
		{
			if(this.hasChild(goal))
				this.detachChild(goal);
		}
		if(showSwitch)
		{
			if(!this.hasChild(goal))
				this.attachChild(goal);
		}
				
	}
}
